//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"WorldMapState.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////

#include "WorldMapState.h"
#include "SelectSquadState.h"
#include "../../SGD Wrappers/CSGD_Direct3D.h"
#include "../../SGD Wrappers/CSGD_DirectInput.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/CSGD_XAudio2.h"
#include "../../SGD Wrappers/CSGD_MessageSystem.h"
#include "../../SGD Wrappers/CSGD_EventSystem.h"
#include "../../SGD Wrappers/SGD_String.h"
#include "../../SGD Wrappers/SGD_Math.h"
#include "../MapSystem/BattleMap.h"
#include "../Game.h"
#include <cassert>
#include "../GameObjects/Player.h"

WorldMapState* WorldMapState::GetInstance()
{
	static WorldMapState Instance;

	return &Instance;
}
WorldMapState::WorldMapState()
{
	cursorX = 0;
	cursorY = 0;
}
WorldMapState::~WorldMapState()
{
}
/*virtual*/ void WorldMapState::Enter()
{
	worldMapImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/worldmapscreen.png"));

	map1ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map1.png"));
	map1_2ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map1_2.png"));
	map1_3ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map1_3.png"));
	map2ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map2.png"));
	map2_2ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map2_2.png"));
	map2_3ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map2_3.png"));
	map3ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map3.png"));
	map3_2ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map3_2.png"));
	map3_3ImgID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/map3_3.png"));
	yUp = false;
	yDown = false;
	xRight = false;
	xLeft = false;

}
/*virtual*/ void WorldMapState::Exit()
{

}
/*virtual*/ bool WorldMapState::Input()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();

	//need to be able to check if they have beaten map 1 or two
	//to be able to move cursor
	if(DI->JoystickIsUnplugged())
	{
		if(DI->KeyPressed( DIK_DOWN) == true)
		{
			++cursorY;
		
			if(cursorY > 2)
				cursorY = 2;
		}
		else if(DI->KeyPressed(DIK_UP) == true)
		{
			--cursorY;
			
			if(cursorY < 0)
				cursorY = 0;
		}

		if(DI->KeyPressed(DIK_LEFT) == true)
		{
			--cursorX;
			
			if(cursorX  < 0)
				cursorX = 0;		
		}
		else if(DI->KeyPressed(DIK_RIGHT) == true)
		{
			++cursorX;

			if(cursorX > 2)
				cursorX = 2;
		}

		if(DI->KeyPressed(DIK_RETURN) == true)
		{
			switch(cursorY)
			{
			case 0:
				{
					if(cursorX == 0)
					{
						selectedMap = map1ImgID;
						levelSlot = 0;
					}
					else if(cursorX == 1)
					{
						selectedMap = map1_2ImgID;
						levelSlot = 1;
					}
					else if(cursorX == 2)
					{
						selectedMap = map1_3ImgID;
						levelSlot = 2;
					}

				}
				break;
			case 1:
				{
					if(cursorX == 0)
					{
						selectedMap = map2ImgID;
						levelSlot = 3;
					}
					else if(cursorX == 1)
					{
						selectedMap = map2_2ImgID;
						levelSlot = 4;
					}
					else if(cursorX == 2)
					{
						selectedMap = map2_3ImgID;
						levelSlot = 5;
					}
				}
				break;
			case 2:
				{
					if(cursorX == 0)
					{
						selectedMap = map3ImgID;
						levelSlot = 6;
					}
					else if(cursorX == 1)
					{
						selectedMap = map3_2ImgID;
						levelSlot = 7;
					}
					else if(cursorX == 2)
					{
						selectedMap = map3_3ImgID;
						levelSlot = 8;
					}
				}
				break;
			}
			Game::GetInstance()->PushState(SelectSquadState::GetInstance());
		}

		if(DI->KeyPressed(DIK_ESCAPE) == true)
		{
			Game::GetInstance()->PopState();
		}
	}
	else
	{
		Keyboard();
	}

	return true;
}
/*virtual*/ void WorldMapState::Update( float fElapsedTime )
{

}
/*virtual*/ void WorldMapState::Render()
{
	BitmapFont* Font = Game::GetInstance()->GetFont();
	//CSGD_Direct3D::GetInstance()->Clear(D3DCOLOR_ARGB(255,50,50,50));

	CSGD_TextureManager::GetInstance()->Draw(worldMapImgID, 0, 0, 0.6f, 0.6f);

	Font->Draw( _T("SELECT MAP"), 
		(int)((340 - (3*24*2.0f)) / 2), 
		50, 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	switch(cursorY)
	{
	case 0:
		{
			if(cursorX == 0)
			{
				CSGD_TextureManager::GetInstance()->Draw(map1ImgID, 450, 200, .15f, .15f);
			}
			else if(cursorX == 1)
			{
				CSGD_TextureManager::GetInstance()->Draw(map1_2ImgID, 500, 300, 1.0f, 1.0f);
			}
			else if(cursorX == 2)
			{
				CSGD_TextureManager::GetInstance()->Draw(map1_3ImgID, 500, 300, 1.0f, 1.0f);
			}

		}
		break;
	case 1:
		{
			if(cursorX == 0)
			{
				CSGD_TextureManager::GetInstance()->Draw(map2ImgID, 500, 300, 1.0f, 1.0f);
			}
			else if(cursorX == 1)
			{
				CSGD_TextureManager::GetInstance()->Draw(map2_2ImgID, 500, 300, 1.0f, 1.0f);
			}
			else if(cursorX == 2)
			{
				CSGD_TextureManager::GetInstance()->Draw(map2_3ImgID, 500, 300, 1.0f, 1.0f);
			}
		}
		break;
	case 2:
		{
			if(cursorX == 0)
			{
				CSGD_TextureManager::GetInstance()->Draw(map3ImgID, 500, 300, 1.0f, 1.0f);
			}
			else if(cursorX == 1)
			{
				CSGD_TextureManager::GetInstance()->Draw(map3_2ImgID, 500, 300, 1.0f, 1.0f);
			}
			else if(cursorX == 2)
			{
				CSGD_TextureManager::GetInstance()->Draw(map3_3ImgID, 500, 300, 1.0f, 1.0f);
			}
		}
		break;
	}


	Font->Draw( _T("( )"), 15 + (cursorX * 100), 200 + (cursorY * 125), 2.0f, D3DCOLOR_ARGB(255, 255, 255, 255) );

}
bool WorldMapState::Keyboard()
{
	CSGD_DirectInput* DI = CSGD_DirectInput::GetInstance();
	if(DI->JoystickGetLStickYNormalized() > 0 && yDown == false)
	{
		++cursorY;
		//if(aA->selected == true)
		//{
		//	aB->selected = true;
		//	aA->selected = false;
		//}
		//else if(bA->selected == true)
		//{
		//	cA->selected = true;
		//	bA->selected = false;
		//}

		if(cursorY > 2)
			cursorY = 2;
		yDown = true;
	}
	else if(DI->JoystickGetLStickYNormalized() < 0 && yUp == false)
	{
		--cursorY;
		//if(bA->selected == true)
		//{
		//	aA->selected = true;
		//	bA->selected = false;
		//}
		//else if(cA->selected == true)
		//{
		//	bA->selected = true;
		//	cA->selected = false;
		//}
		if(cursorY < 0)
			cursorY = 0;
		yUp = true;
	}

	if(DI->JoystickGetLStickXNormalized() < 0 && xLeft == false)
	{
		--cursorX;
		//if(aB->selected == true)
		//{
		//	aB->prev->selected = true;
		//	aB->selected = false;
		//}
		//else if(aC->selected == true)
		//{
		//	aC->prev->selected = true;
		//	aC->selected = false;
		//}

		//if(bB->selected == true)
		//{
		//	bB->prev->selected = true;
		//	bB->selected = false;
		//}
		//else if(bC->selected == true)
		//{
		//	bC->prev->selected = true;
		//	bC->selected = false;
		//}

		//if(cB->selected == true)
		//{
		//	cB->prev->selected = true;
		//	cB->selected = false;
		//}
		//else if(cC->selected == true)
		//{
		//	cC->prev->selected = true;
		//	cC->selected = false;
		//}

		if(cursorX  < 0)
			cursorX = 0;		
		xLeft = true;
	}
	else if(DI->JoystickGetLStickXNormalized() > 0 && xRight == false)
	{
		++cursorX;

		//if(aB->selected == true)
		//{
		//	aB->next->selected = true;
		//	aB->selected = false;
		//}
		//else if(aA->selected == true)
		//{
		//	aA->next->selected = true;
		//	aA->selected = false;
		//}

		//if(bB->selected == true)
		//{
		//	bB->next->selected = true;
		//	bB->selected = false;
		//}
		//else if(bA->selected == true)
		//{
		//	bA->next->selected = true;
		//	bA->selected = false;
		//}

		//if(cB->selected == true)
		//{
		//	cB->next->selected = true;
		//	cB->selected = false;
		//}
		//else if(cA->selected == true)
		//{
		//	cA->next->selected = true;
		//	cA->selected = false;
		//}

		if(cursorX > 2)
			cursorX = 2;
		xRight = true;
	}

	if(DI->JoystickButtonPressed(0) == true)
	{
		switch(cursorY)
		{
		case 0:
			{
				if(cursorX == 0)
				{
					selectedMap = map1ImgID;
				}
				else if(cursorX == 1)
				{
					selectedMap = map1_2ImgID;
				}
				else if(cursorX == 2)
				{
					selectedMap = map1_3ImgID;
				}

			}
			break;
		case 1:
			{
				if(cursorX == 0)
				{
					selectedMap = map2ImgID;
				}
				else if(cursorX == 1)
				{
					selectedMap = map2_2ImgID;
				}
				else if(cursorX == 2)
				{
					selectedMap = map2_3ImgID;
				}
			}
			break;
		case 2:
			{
				if(cursorX == 0)
				{
					selectedMap = map3ImgID;
				}
				else if(cursorX == 1)
				{
					selectedMap = map3_2ImgID;
				}
				else if(cursorX == 2)
				{
					selectedMap = map3_3ImgID;
				}
			}
			break;
		}
		Game::GetInstance()->PushState(SelectSquadState::GetInstance());
	}

	if(DI->JoystickButtonPressed(1) == true)
	{
		Game::GetInstance()->PopState();
	}
	if(DI->JoystickGetLStickYNormalized() == 0)
	{
		yUp = false;
		yDown = false;
	}
	if(DI->JoystickGetLStickXNormalized() == 0)
	{
		xRight = false;
		xLeft = false;
	}
	return true;
}